/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.util.common;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author Administrator
 */
public class JsonBuilder {

    /**
     * @param datali
     * @return String 注意 datali 的数据格式 列头 为: c1,c2,c3......
     */
    public static String getJson(List<HashMap> datali, List<Integer> ignoreColumns) {
        if (null == datali) {
            return null;
        }
        if (null == ignoreColumns) {
            return getJson(datali);
        }
        DecimalFormat format = new DecimalFormat("#,##0.00");
        StringBuilder builder = new StringBuilder();
        String mark = "\",\"";
        int size = datali.size();
        builder.append("{\"page\":1").append(",\"total\":").append(size).append(",\"records\":").append(size).append(",\"rows\":[");
        for (int i = 0; i < size; i++) {
            HashMap row = datali.get(i);
            builder.append("{\"id\":");
            builder.append(i + 1);
            builder.append(",\"cell\":[\"");
            for (int j = 1; j < row.entrySet().size() + 1; j++) {
                if (!ignoreColumns.contains(j)) {
                    Object col_data = row.get("c" + j);
                    if (col_data instanceof Double) {
                        col_data = format.format(col_data);
                    }
                    builder.append(col_data).append(mark);
                }
            }
            builder.append("\"]}");
            if (i < size - 1) {
                builder.append(",");
            }
        }
        builder.append("]}");
        return builder.toString();
    }

    /**
     * @param datali
     * @return String 注意 datali 的数据格式 列头 为: c1,c2,c3...... ignoreColumns 要忽略的列 :
     * 请传入一个包含要忽略的列 的 序号的列表,这些列 将不会加入到 json 数据中
     */
    public static String getJson(List<HashMap> datali) {
        if (null == datali) {
            return null;
        }
        DecimalFormat format = new DecimalFormat("#,##0.00");
        StringBuilder builder = new StringBuilder();
        String mark = "\",\"";
        int size = datali.size();
        builder.append("{\"page\":1").append(",\"total\":").append(size).append(",\"records\":").append(size).append(",\"rows\":[");
        for (int i = 0; i < size; i++) {
            HashMap row = datali.get(i);
            builder.append("{\"id\":");
            builder.append(i + 1);
            builder.append(",\"cell\":[\"");
            for (int j = 1; j < row.entrySet().size() + 1; j++) {
                Object col_data = row.get("c" + j);
                if (col_data instanceof Double) {
                    col_data = format.format(col_data);
                }
                builder.append(col_data).append(mark);
            }
            builder.append("\"]}");
            if (i < size - 1) {
                builder.append(",");
            }
        }
        builder.append("]}");
        return builder.toString();
    }

    public static void main(String[] args) {
        Map<Integer, Double> m = new TreeMap<Integer, Double>();
        m.put(1, 2.0);
        m.put(2, 1.0);
        m.put(3, 4.0);
        Object o[] = m.values().toArray();
        for (int i = 0; i < o.length; i++) {
            System.out.println(o[i]);
        }
    }
}
